package com.atguigu.flink.chapter10.query;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.kerby.config.Conf;

/*
自定义source
多维分析：
    去重 distinct
    多维分析  group by grouping sets
    上钻    group by rollup
    多为立方体 group bu cube
 */
public class Query1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

        //自定义source
//        tEnv.sqlQuery("select a,b from (VALUES (1,2.0),(2,3.1)) as  t(a,b)").execute().print();

        tEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int" +
                ") with(" +
                " 'connector' = 'kafka', " +
                "  'topic' = 's1', " +
                "  'properties.bootstrap.servers' = 'hadoop102:9092', " +
                "  'properties.group.id' = 'atguigu', " +
                "  'scan.startup.mode' = 'latest-offset', " +
                "  'format' = 'csv' " +
                ")");

        //去重
        //tEnv.sqlQuery("select distinct id from sensor").execute().print();

        //多维度分析
        /*tEnv.sqlQuery("select id,ts,sum(vc)  vc_sum" +
                "from sensor " +
                "group by grouping sets(" +
                "(id,ts)," +
                "(id)," +
                "( )" +
                ")").execute().print();
         */

        //上钻
        /*tEnv.sqlQuery("select id,ts,sum(vc)  vc_sum " +
                "from sensor " +
                "group by rollup(" +
                "id,ts" +
                ")").execute().print();
         */


        //多维立方体
        /*tEnv.sqlQuery("select id,ts,sum(vc) vc_sum " +
                "from sensor " +
                "group by cube(" +  //等价于(id,ts) (id) (ts) ()
                "id,ts" +
                ")").execute().print();
         */

    }
}


//public class Query1 {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
////        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        DataStreamSource<Tuple2<String, Long>> source = env.fromElements(
//                Tuple2.of("a", 1L),
//                Tuple2.of("b", 1L)
//        );
//
//        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
//
//        //自定义source
//        //tEnv.sqlQuery("select a,b from (VALUES(1,2.1),(2,3.1)) as t(a,b)").execute().print();
//
//        tEnv.executeSql("create table sensor(" +
//                "id string," +
//                "ts bigint," +
//                "vc int" +
//                ") with(" +
//                " 'connector' = 'kafka', " +
//                "  'topic' = 's1', " +
//                "  'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                "  'properties.group.id' = 'atguigu', " +
//                "  'scan.startup.mode' = 'latest-offset', " +
//                "  'format' = 'csv' " +
//                ")");
//
//
//        //去重
//        //tEnv.sqlQuery("select distinct id from sensor").execute().print();
//
//        //多维分析
////        tEnv.sqlQuery("select id,ts,sum(vc) vc_sum " +
////                " from sensor " +
////                "group by grouping sets (" +
////                "(id,ts)," +
////                "(id)," +
////                "()" +
////                ")").execute().print();
//
//
//        //上钻
//                tEnv.sqlQuery("select id,ts,sum(vc) vc_sum " +
//                " from sensor " +
//                "group by rollup(" +
//                "id,ts" +
//                ")").execute().print();
//
//        //多维立方体
//        tEnv.sqlQuery("select id,ts,sum(vc) vc_sum " +
//                " from sensor " +
//                "group by cube(" +
//                "id,ts" +
//                ")").execute().print();
//    }
//}
